package Java.view.game.douniu;

import Java.view.Common.GameFX;
import domain.User;
import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.io.IOException;
import java.sql.SQLException;

public class douniuTeamFX extends Application {
    String grade = "", usernameText = "";
    int dou = 0, exprience = 0, max = 0, i = 0;
    boolean flag;
    User user;

    Button toReturn = new Button(); // 返回主界面
    Button music = new Button(); // 音乐控制
    Button setting = new Button(); // 头像
    Button danji = new Button(); // 单机
    Button zudui = new Button(); // 组队
    Label username = new Label(); // 用户名
    ProgressBar bar = new ProgressBar(); // 经验条
    Label gra = new Label(); // 等级title
    Label exp = new Label(); // 经验
    Label money = new Label(); // 欢乐豆
    Button happyDou = new Button(); // 充值
    public douniuTeamFX(){
        this.user=new User("123","123",888,888);
        //new teamFX(user,1);
    }

    public douniuTeamFX(User user1) {

        user = user1;
        int temp = user.getExp();
        usernameText = user.getUsername();

        // 等级和经验逻辑
        if (temp < 10) {
            grade = "短工";
            exprience = temp;
            max = 10;
        } else if (temp < 20) {
            grade = "长工";
            exprience = temp - 10;
            max = 10;
        } else if (temp < 40) {
            grade = "佃户";
            max = 20;
            exprience = temp - 20;
        } else if (temp < 60) {
            grade = "贫农";
            exprience = temp - 40;
            max = 20;
        } else if (temp < 100) {
            grade = "渔夫";
            exprience = temp - 60;
            max = 40;
        } else if (temp < 200) {
            grade = "猎人";
            exprience = temp - 100;
            max = 100;
        } else if (temp < 400) {
            grade = "中农";
            exprience = temp - 200;
            max = 200;
        } else if (temp < 600) {
            grade = "富农";
            exprience = temp - 400;
            max = 200;
        } else if (temp < 1000) {
            grade = "掌柜";
            exprience = temp - 600;
            max = 400;
        } else if (temp < 2000) {
            grade = "商人";
            exprience = temp - 1000;
            max = 1000;
        } else if (temp < 5000) {
            grade = "衙役";
            exprience = temp - 2000;
            max = 3000;
        } else if (temp < 10000) {
            grade = "小财主";
            exprience = temp - 5000;
            max = 5000;
        } else if (temp < 20000) {
            grade = "大财主";
            exprience = temp - 10000;
            max = 10000;
        } else if (temp < 50000) {
            grade = "小地主";
            exprience = temp - 20000;
            max = 30000;
        } else if (temp < 100000) {
            grade = "大地主";
            exprience = temp - 50000;
            max = 50000;
        } else {
            grade = "知县";
            exprience = temp - 100000;
        }
        dou = user.getHappyDou();
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("游戏首页");
        primaryStage.getIcons().add(new Image("file:src/image/dizhu.png"));

        Pane root = new Pane();
        Scene scene = new Scene(root, 800, 600);
        usernameText=user.getUsername();
        dou=user.getHappyDou();

        // 背景
        ImageView background = new ImageView(new Image("file:src/image/主界面背景.jpg"));
        background.setFitWidth(800);
        background.setFitHeight(600);
        root.getChildren().add(background);

        // 用户名、等级、经验条和欢乐豆
        username.setText(usernameText);
        username.setFont(Font.font(null, 20));
        username.setStyle("-fx-text-fill: orange;");
        username.setLayoutX(600);
        username.setLayoutY(16);

        gra.setText(grade);
        gra.setFont(Font.font(null, 16));
        gra.setStyle("-fx-text-fill: black;");
        gra.setLayoutX(670);
        gra.setLayoutY(11);

        exp.setText(exprience + "/" + max);
        exp.setFont(Font.font(null, 10));
        exp.setStyle("-fx-text-fill: black;");
        exp.setLayoutX(670);
        exp.setLayoutY(26);

        money.setText(String.valueOf(dou));
        money.setFont(Font.font(null, 24));
        money.setStyle("-fx-text-fill: black;");
        money.setLayoutX(370);
        money.setLayoutY(565);

        bar.setProgress(exprience / (double) max);
        bar.setLayoutX(600);
        bar.setLayoutY(43);
        bar.setPrefWidth(100);

        root.getChildren().addAll(username, gra, exp, happyDou, bar);

        // 按钮设置
        setupButton(toReturn, "src/image/返回.png", 15, 8);
        setupButton(music, flag ? "src/image/喇叭.png" : "src/image/静音.png", 80, 6);
        setupButton(setting, "src/image/dizhu.png", 720, 8);
        setupButton(danji, "src/image/login/单机模式.png", 100, 190);
        setupButton(zudui, "src/image/login/组队模式.png", 450, 190);
        setupButton(happyDou, "src/image/欢乐豆.png", 275, 535);

        root.getChildren().addAll(toReturn, music, setting, danji, zudui, money);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void setupButton(Button button, String iconPath, double x, double y) {
        ImageView icon = new ImageView(new Image("file:" + iconPath));
        button.setGraphic(icon);
        button.setStyle("-fx-background-color: transparent;");
        button.setLayoutX(x);
        button.setLayoutY(y);
        button.setOnMouseClicked(e -> {
            try {
                handleButtonClick(button);
            } catch (IOException | SQLException ex) {
                throw new RuntimeException(ex);
            }
        });
        button.setOnMouseEntered(event -> button.setCursor(Cursor.HAND));

        // 设置鼠标退出事件，光标恢复为默认
        button.setOnMouseExited(event -> button.setCursor(Cursor.DEFAULT));
    }

    private void handleButtonClick(Button button) throws IOException, SQLException {
        if (button == toReturn) {
            new GameFX(user).start(new Stage());
            ((Stage) toReturn.getScene().getWindow()).close();
        } else if (button == danji) {
            new douniuFX(user).start(new Stage());
            ((Stage) danji.getScene().getWindow()).close();

        } else if (button == zudui) {
            new douniuFX_lian(user).start(new Stage());
            ((Stage) zudui.getScene().getWindow()).close();

        } else if (button == music) {
            // 音乐控制逻辑
            flag = !flag;
            music.setGraphic(new ImageView(new Image("file:" + (flag ? "src/image/喇叭.png" : "src/image/静音.png"))));
        } else if (button == happyDou) {
            // 充值逻辑
            System.out.println("Recharge");
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
